package com.gouying.web.controller;

import com.gouying.common.utils.R;
import com.gouying.web.service.SeckillService;
import com.gouying.web.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 1、整合sentinel
 * 1)、导入依赖
 * <dependency>
 * <groupId>com.alibaba.cloud</groupId>
 * <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 * </dependency>
 * 2)、下载sentinel的控制台
 * 3)、配置sentinel控制台地址信息
 * 4)、在控制台调整参数。【默认所有的流控设置保存在内存中，重启失效】
 * 2、每一个微服务都导入，可以图形化显示每一个服务的请求信息
 * <dependency>
 * <groupId>org.springframework.boot</groupId>
 * <artifactId>spring-boot-starter-actuator</artifactId>
 * </dependency>
 * 并配置management.endpoints.web.exposure.include=*
 *
 * @author ly
 */
@Controller
public class SecKillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSecKillSkus() {
        List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 获取秒杀商品信息
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 真正点击秒杀按钮进行秒杀
     *
     * @param killId
     * @param key
     * @param num
     * @param model
     * @return
     */
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("", orderSn);
        //1、判断是否登录
        return "success";
    }
}
